<?php

function reequipment_userapi_getcompanyschedule($args)
{
    extract($args);

    // Optional arguments
    if (!isset($startnum)) {$startnum = 1;}
    if (!isset($numitems)) {$numitems = -1;}

    $dbconn =& xarDBGetConn();
    $xartable =& xarDBGetTables();
    $table = $xartable['recompanyschedule'];
    $sjoin = $xartable['rescheduled'];
    $rjoin = $xartable['rel_eq'];
    $ajoin = $xartable['reareas'];
    $mjoin = $xartable['remodels'];
    $ejoin = $xartable['reequipment'];

    $query = "SELECT cs.schedule_id, 
    				 cs.company_id, 
    				 cs.equipment_id,
    				 cs.last_scheduled, 
    				 cs.last_completed,
    				 r.company_name,
    				 s.model_id,
    				 s.area_id,
    				 a.area_name,
    				 m.model_name,    				 
    				 e.serial_number, 
    				 e.cycles"
    		. " FROM $table cs"
    		
    		. " LEFT OUTER JOIN $sjoin s"
    		. " on cs.schedule_id = s.schedule_id"

    		. " LEFT OUTER JOIN $rjoin r"
    		. " on cs.company_id = r.company_id"
    		
    		. " LEFT OUTER JOIN $mjoin m"
    		. " on cs.model_id = m.model_id"
    		
    		. " LEFT OUTER JOIN $ajoin a"
    		. " on m.area_id = a.area_id"

    		. " LEFT OUTER JOIN $ejoin e"
    		. " on m.model_id = e.model_id";	
    		
    		
    $where = array();
    $bind = array();

    if (!empty($schedule_id)) {
        $where[] = 'cs.schedule_id = ?';
        $bind[] = (int)$schedule_id;
    }
    
    if (!empty($company_id)) {
        $where[] = 'cs.company_id = ?';
        $bind[] = (int)$company_id;
    }

    if (!empty($equipment_id)) {
        $where[] = 'e.equipment_id = ?';
        $bind[] = (int)$equipment_id;
    }
    
    // Figure out the time queries when I get there.       

    if (!empty($where)) {
        $query .= ' WHERE ' . implode(' AND ', $where);
    }

    // Not really necessary
    $query .= " ORDER BY e.equipment_id";
    
    // return $query;

    $result =& $dbconn->SelectLimit($query, $numitems, $startnum-1, $bind);
    if (!$result) return;

    $tags = array();
    for (; !$result->EOF; $result->MoveNext()) {
        list($schedule_id, $company_id, $equipment_id, $last_scheduled, $last_completed, $company_name, $model_id, $area_id, $area_name, $model_name, $serial_number, $cycles) = $result->fields;

        $tags[] = array(
        cs.schedule_id, 
    				 cs.company_id, 
    				 cs.equipment_id,
    				 cs.last_scheduled, 
    				 cs.last_completed,
    				 r.company_name,
    				 s.model_id,
    				 s.area_id,
    				 a.area_name,
    				 m.model_name,    				 
    				 e.serial_number, 
    				 e.cycles
                'schedule_id'	=> $schedule_id,
                'company_id'    => $company_id,
                'equipment_id'	=> $equipment_id,
                'last_scheduled'=> $last_scheduled,
                'last_completed'=> $last_completed,
                'company_name'	=> xarVarPrepForDisplay($company_name),
                'model_id'      => $model_id,
                'area_id'       => $area_id,
                'area_name'		=> xarVarPrepForDisplay($area_name), 
                'model_name'	=> xarVarPrepForDisplay($model_name),                 
                'serial_number'	=> xarVarPrepForDisplay($serial_number),     
                'cycles'    	=> $cycles
        );
    }

    $result->Close();
    return $tags;
}
?>